﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using MyException;

namespace Stationery_Store_Inventory_Management
{
    public partial class AllocateItemToDept : System.Web.UI.Page
    {
        StoreRetrievalController sCon;
        String employeeID;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 5)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                        employeeID = list[4]; //get the current employee
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                sCon = new StoreRetrievalController();
                if (!IsPostBack)
                {
                    sCon.AutoAllocateActualQtyCont();
                    var result = sCon.getStoreRetrievalListCont();

                    storeRetrievalGrid.DataSource = result;
                    storeRetrievalGrid.DataBind();

                    for (int i = 0; i < storeRetrievalGrid.Rows.Count; i++)
                    {
                        if (Convert.ToInt32(storeRetrievalGrid.Rows[i].Cells[4].Text) <= Convert.ToInt32(storeRetrievalGrid.Rows[i].Cells[5].Text))
                        {
                            DataControlFieldCell btnAllocate = (DataControlFieldCell)storeRetrievalGrid.Rows[i].Controls[0];
                            btnAllocate.Enabled = false;
                        }
                        else
                        {
                            if (Convert.ToInt32(storeRetrievalGrid.Rows[i].Cells[6].Text) <= Convert.ToInt32(storeRetrievalGrid.Rows[i].Cells[5].Text))
                            {
                                DataControlFieldCell btnAllocate = (DataControlFieldCell)storeRetrievalGrid.Rows[i].Controls[0];
                                btnAllocate.Enabled = false;
                            }
                        }
                    }

                    // Block the buttons to prevent multiple clicks by the user
                    btnWarehouseConfirm.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnWarehouseConfirm).ToString() + ";");

                    lblMessage.Text = "";
                }

                if (storeRetrievalGrid.Rows.Count <= 0)
                {
                    lblMessage.Text = "There is no item for retrieval currently";
                    btnWarehouseConfirm.Visible = false;
                    btnRaiseAdj.Visible = false;
                }
            }
            catch (Exception ex)
            {
                btnWarehouseConfirm.Enabled = true;
            }
        }

        protected void btnAllocate_Click(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "btnAllocate")
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    string ItemCode = storeRetrievalGrid.Rows[index].Cells[2].Text;
                    Response.Redirect("ItemWiseDeptList.aspx?ic=" + ItemCode);
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnWarehouseConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < storeRetrievalGrid.Rows.Count; i++)
                {
                    DataControlFieldCell btnAllocate = (DataControlFieldCell)storeRetrievalGrid.Rows[i].Controls[0];
                    if (btnAllocate.Enabled == true)
                    {
                        throw new AvailableQtyLessThanRequestedQtyException();
                    }
                }

                sCon.ConfirmWarehouseCollectionCont(employeeID);
                //Response.Write("Warehouse Collection details have been updated");
                sCon.AutoAllocateActualQtyCont();
                var result = sCon.getStoreRetrievalListCont();
                storeRetrievalGrid.DataSource = result;
                storeRetrievalGrid.DataBind();
                btnWarehouseConfirm.Visible = false;
                btnRaiseAdj.Visible = false;
                lblMessage.Text = "Warehouse collection details have been updated";
            }
            catch (AvailableQtyLessThanRequestedQtyException ex)
            {
                btnWarehouseConfirm.Enabled = true;
                lblMessage.Text = "There are items where the Available Quantity is less than the Requested Quantity and the Actual Quantity is more than the Available Quantity. Please allocate the items before proceeding.";
            }
            catch (Exception ex)
            {
                btnWarehouseConfirm.Enabled = true;
            }
        }

        protected void btnRaiseAdj_Click(object sender, EventArgs e)
        {
            try
            {
                string comeFrom = "Store Retrieval";
                Session["ComeFrom"] = comeFrom;
                Response.Redirect("/IssueStockAdjustment.aspx");
            }
            catch (Exception ex)
            {
            }
        }

    }
}